﻿using Scriban;
using Scriban.Parsing;
using Scriban.Runtime;
using System.Reflection;
using System.Text;

namespace EasyXaf.EntityDesigner.CodeTemplates;

public class IncludeTemplateFromEmbedded : ITemplateLoader
{
    public string GetPath(TemplateContext context, SourceSpan callerSpan, string templateName)
    {
        return $"{typeof(IncludeTemplateFromEmbedded).Namespace}.Templates.{templateName}.tpl";
    }

    public string Load(TemplateContext context, SourceSpan callerSpan, string templatePath)
    {
        var assembly = Assembly.GetExecutingAssembly();
        var resourceStream = assembly.GetManifestResourceStream(templatePath);
        using var reader = new StreamReader(resourceStream, Encoding.UTF8);
        return reader.ReadToEnd();
    }

    public async ValueTask<string> LoadAsync(TemplateContext context, SourceSpan callerSpan, string templatePath)
    {
        return await File.ReadAllTextAsync(templatePath, Encoding.UTF8);
    }
}
